package com.example.controller;

import com.example.pojo.Result;
import com.example.util.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/api")
public class MinioController {
    @Autowired
    MinioUtils minioUtils;
    @Value("${spring.minio.bucket-name}")
    String bucketName;
    @Value("${spring.minio.url}")
    String url;


    @PostMapping("/upload")
    public Result uploadAvatar(@RequestParam MultipartFile file){
        //修改文件名------随机文件名
        String name = UUID.randomUUID().toString();
        String fileName = name.substring(0, 5);
        //上传文件到指定服务器中 ------工具类
        minioUtils.upload(file,fileName);
        //返回给前端该图片服务器地址   Ip:端口/桶名/文件名
        String path=url+"/"+bucketName+"/"+fileName;
        System.out.println(path);
        return Result.ok(path,"上传成功",200);
    }
}
